<template>
  <div class="article-container">
    <!-- 导航栏 -->
    <van-nav-bar
      fixed
      left-arrow
      @click-left="$router.back()"
      title="文章详情"
    ></van-nav-bar>
    <!-- /导航栏 -->

    <!-- 加载中 loading -->
    <van-loading v-if="loading" class="article-loading" />
    <!-- /加载中 loading -->

    <!-- 文章详情 -->
    <div class="detail">
      <h3 class="title">{{article.title}}</h3>
      <div class="author">
        <van-image round width="1rem" height="1rem" fit="fill" :src="article.aut_photo" />
        <div class="text">
          <p class="name">{{article.aut_name}}</p>
          <p class="time">{{article.pubdate | relTime}}</p>
        </div>
        <van-button
          round
          size="small"
          type="info"
          @click="hSwitchFollow"
        >{{ article.is_followed ? '取关' : '+ 关注' }}</van-button>
      </div>
      <div class="content">
        <div v-html="article.content"></div>
      </div>
      <van-divider>END</van-divider>
      <div class="zan">
        <van-button
        round size="small"
        hairline
        type="primary"
        plain
        icon="good-job-o"
        @click="hSwitchLike"
        >{{article.attitude === 1 ? '取消点赞' : '点赞'}}</van-button>
        &nbsp;&nbsp;&nbsp;&nbsp;
        <van-button
        round size="small"
        hairline type="danger"
        plain
        icon="delete"
        @click="hSwitchDisLike"
        >{{article.attitude === 0 ? '取消不喜欢' : '不喜欢'}}</van-button>
      </div>
    </div>
    <!-- /文章详情 -->

    <!-- 文章评论 -->
    <article-comment
     ref="refsContent"
    :articleId="articleId"
    ></article-comment>
  </div>
</template>

<script>
import { getArticle, addLike, deleteLike, addDislike, deleteDislike } from '@/api/article.js'
import { followUser, unFollowUser } from '@/api/user.js'
import ArticleComment from './comment'
export default {
  name: 'ArticleIndex',
  components: {
    ArticleComment
  },
  data () {
    return {
      loading: true, // 控制加载中的 loading 状态
      article: {}, // 当前文章
      articleId: null
    }
  },
  created () {
    this.articleId = this.$route.params.id
    this.loadArticle()
  },
  methods: {
    // 喜欢和不喜欢
    async hSwitchDisLike () {
      // 如果attitude是0，即当前给文章 添加了 不喜欢，则点击动作就是要 取消 不喜欢
      if (this.article.attitude === 0) {
        await deleteDislike(this.article.art_id.toString())
        this.article.attitude = -1
      } else {
        await addDislike(this.article.art_id.toString())
        this.article.attitude = 0
      }
      this.$toast.success('操作成功')
    },
    // 点赞和取消点赞
    async hSwitchLike () {
      // 如果attitude是1，即当前给文章点赞的，则点击动作就是要 取消点赞
      if (this.article.attitude === 1) {
        await deleteLike(this.article.art_id.toString())
      } else {
        await addLike(this.article.art_id.toString())
      }
      this.$toast.success('操作成功')
      this.article.attitude = this.article.attitude * -1
    },
    // 关注和取消关注   如果文章是自己的 则不能自己关注自己
    async hSwitchFollow () {
      const isFollowed = this.article.is_followed
      const autId = this.article.aut_id
      if (isFollowed) {
        // 如果 is_followed是true,在点击按钮，说明要做取关
        try {
          await unFollowUser(autId)
          this.article.is_followed = false
          this.$toast.success('取关成功')
        } catch (err) {
          this.$toast.fail('取关失败')
        }
      } else {
        // 如果 is_followed是false,在点击按钮，说明要做加关注
        try {
          await followUser(autId)
          this.article.is_followed = true
          this.$toast.success('加关注成功')
        } catch (err) {
          this.$toast.fail('加关注失败')
        }
      }
    },
    // 获取文章详情
    async loadArticle () {
      const res = await getArticle(this.$route.params.id)
      console.log('res', res)
      this.article = res.data.data
      this.loading = false
    }
  },
  // 组件级别的路由守卫
  beforeRouteLeave (to, from, next) {
    if (this.$refs.refsContent.content) {
      const cns = window.confirm('你还没有发表评论，确定离开吗？')
      if (cns) {
        next()
      } else {
        // 页面不跳转
        return false
      }
    } else {
      next()
    }
  }
}
</script>

<style scoped lang='less'>
.article-container{
  position: absolute;
  left: 0;
  top: 0;
  overflow-y: scroll;
  width: 100%;
  height: 100%;
}
.article-loading {
  padding-top: 100px;
  text-align: center;
}
.error{
  padding-top: 100px;
  text-align: center;
}
.detail {
  padding: 50px 10px;
  .title {
    font-size: 16px;
  }
  .zan{
    text-align: center;
  }
  .author {
    padding: 10px 0;
    display: flex;
    .text {
      flex: 1;
      padding-left: 10px;
      line-height: 1.3;
      .name {
        font-size: 14px;
        margin: 0;
      }
      .time {
        margin: 0;
        font-size: 12px;
        color: #999;
      }
    }
  }
  .content {
    font-size:14px;
    overflow: hidden;
    white-space: pre-wrap;
    word-break: break-all;
    /deep/ img{
      max-width:100%;
      background: #f9f9f9;
    }
  }
}
</style>
